import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldString;
	private JTextField textFieldPattern;
	private JLabel lblMassage;
	private PatternMatching p;
	private JButton btnExample;
	private String[] es = { "cdbbacc", "aaaaaaaaaa" };
	private String[] et = { "abcd", "aaaab" };
	private int ei = 0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Brute-Force");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblString = new JLabel("String:");
		lblString.setHorizontalAlignment(SwingConstants.RIGHT);
		lblString.setBounds(14, 34, 72, 18);
		contentPane.add(lblString);

		textFieldString = new JTextField();
		textFieldString.setToolTipText("");
		textFieldString.setText("cdbbacc");
		textFieldString.setBounds(94, 31, 221, 24);
		contentPane.add(textFieldString);
		textFieldString.setColumns(10);

		JLabel lblPattern = new JLabel("Pattern:");
		lblPattern.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPattern.setBounds(14, 86, 72, 18);
		contentPane.add(lblPattern);

		textFieldPattern = new JTextField();
		textFieldPattern.setText("abcd");
		textFieldPattern.setBounds(94, 83, 221, 24);
		contentPane.add(textFieldPattern);
		textFieldPattern.setColumns(10);

		JButton btnMatching = new JButton("Match");
		btnMatching.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnMatchingCall();
			}
		});
		btnMatching.setBounds(155, 138, 113, 27);
		contentPane.add(btnMatching);

		lblMassage = new JLabel("Input parameters and click the button!");
		lblMassage.setHorizontalAlignment(SwingConstants.CENTER);
		lblMassage.setBounds(14, 199, 404, 18);
		contentPane.add(lblMassage);

		btnExample = new JButton("Example");
		btnExample.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnExampleCall();
			}
		});
		btnExample.setBounds(329, 53, 89, 27);
		contentPane.add(btnExample);
	}

	protected void btnExampleCall() {
		ei++;
		if (ei >= es.length) {
			ei = 0;
		}
		textFieldString.setText(es[ei]);
		textFieldPattern.setText(et[ei]);
	}

	protected void btnMatchingCall() {
		String s = textFieldString.getText();
		String t = textFieldPattern.getText();
		try {
			p = new PatternMatching(s, t);
		} catch (Exception e) {
			lblMassage.setText(e.getMessage());
			return;
		}
		int bfCount = p.getBfCount();
		int index = p.getIndex();
		if (index >= 0) {
			lblMassage.setText("After " + bfCount + " times contrast, it is match, and the index is " + index + "!");
		} else {
			lblMassage.setText("After " + bfCount + " times contrast, it is not match!");
		}
	}
}
